<template>
  <div>
    <BasicModal
      @register="toCustomers"
      v-bind="$attrs"
      :helpMessage="['提示1', '提示2']"
      @ok="toCustomersClick"
    >
      <BasicForm @register="toCustomersRegisterForm" />
    </BasicModal>
  </div>
</template>

<script setup lang="ts">
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { getCustomersList, customerToEdge } from '/@/api/thingsborad/edge/edge';
  import { edgeParams } from '/@/api/thingsborad/edge/model/edgeModel';
  import { ref } from 'vue';
  const emits = defineEmits(['closeFlag']);
  let params = ref<edgeParams>({
    pageSize: 50,
    page: 0,
    sortProperty: 'title',
    sortOrder: 'ASC',
  });
  let theEdgeData = ref<any>();

  let addSchemas: FormSchema[] = [
    {
      field: 'toCustomersType',
      component: 'Select',
      label: '请选择需要分配给边缘的客户',
      required: true,
      colProps: {
        span: 24,
      },
    },
  ];

  const [toCustomers] = useModalInner(async (data) => {
    try {
      theEdgeData.value = data;
      params.value.textSearch = '';
      const customerList = await getCustomersList(params.value);
      let selectDataList = new Array<any>();
      customerList.data.forEach((item) => {
        selectDataList.push({
          label: item.name,
          value: item.name,
        });
      });
      updateSchema({
        field: 'toCustomersType',
        componentProps: {
          options: selectDataList,
        },
      });
    } catch (error) {
      
    }
  });
  // 表单初始化
  const [toCustomersRegisterForm, { validate, updateSchema, resetFields }] = useForm({
    layout: 'vertical',
    schemas: addSchemas,
    actionColOptions: {
      span: 24,
    },
    showActionButtonGroup: false,
  });
  async function toCustomersClick() {
    try {
      const validateValue = await validate();
      params.value.textSearch = validateValue.toCustomersType;
      const theCustomers = await getCustomersList(params.value);
      

      let data: any = {
        id: theCustomers.data[0].id.id + '/edge/' + theEdgeData.value.id,
        data: {
          headers: {
            normalizedNames: {},
            lazyUpdate: null,
          },
          params: {
            updates: null,
            cloneFrom: null,
            encoder: {},
            map: null,
            interceptorConfig: {
              ignoreLoading: false,
              ignoreErrors: false,
              resendRequest: false,
            },
          },
        },
      };
      await customerToEdge(data);
      resetFields();
      emits('closeFlag', true);
    } catch (error) {
      
    }
  }
</script>
